package com.wasion.pwportable.protocol.analyzer;

import java.math.BigDecimal;

import com.wasion.pwportable.protocol.Result;

public abstract class AbstractAnalyzer implements AnalyzerIF {

	protected Result result;
	private String actionId;

	@Override
	public Result analyze(String ackBS) {
		result = new Result();

		dataAnalysis(ackBS);

		return result;
	}

	// 具体解析数据域的方法
	public abstract void dataAnalysis(String ackBS);

	/**
	 * @param laoddata
	 * @return
	 * @throws NumberFormatException
	 *             逆序解析的电流
	 */
	public double getCurrentInverted(String loaddata)
			throws NumberFormatException {

		String loadByte = loaddata.substring(2, 4) + loaddata.substring(0, 2);

		String loadBinery = Integer.toBinaryString(Integer.parseInt(loadByte,
				16)); // 传唤成2进制 字符串
		// 高位补0
		while (loadBinery.length() < 16) {
			loadBinery = "0" + loadBinery;
		}

		String param = loadBinery.substring(0, 2);
		String laodData = loadBinery.substring(2, 16);
		double load = 0.0;
		if (param.equals("00")) {
			load = Integer.parseInt(laodData, 2) / 2.0;
		} else if (param.equals("01")) {
			load = Integer.parseInt(laodData, 2) / 8.0;
		} else if (param.equals("10")) {
			load = Integer.parseInt(laodData, 2) / 64.0;
		} else if (param.equals("11")) {
			load = Integer.parseInt(laodData, 2) / 128.0;
		}
		BigDecimal round = new BigDecimal(load);
		return round.setScale(3, BigDecimal.ROUND_HALF_EVEN).doubleValue();
	}

	// 正序解析的电流
	public double getCurrentPositive(String loaddata)
			throws NumberFormatException {

		String loadBinery = Integer.toBinaryString(Integer.parseInt(loaddata,
				16)); // 传唤成2进制 字符串
						// 高位补0
		while (loadBinery.length() < 16) {
			loadBinery = "0" + loadBinery;
		}

		String param = loadBinery.substring(0, 2);
		String laodData = loadBinery.substring(2, 16);
		double load = 0.0;
		if (param.equals("00")) {
			load = Integer.parseInt(laodData, 2) / 2.0;
		} else if (param.equals("01")) {
			load = Integer.parseInt(laodData, 2) / 8.0;
		} else if (param.equals("10")) {
			load = Integer.parseInt(laodData, 2) / 64.0;
		} else if (param.equals("11")) {
			load = Integer.parseInt(laodData, 2) / 128.0;
		}
		BigDecimal round = new BigDecimal(load);
		return round.setScale(3, BigDecimal.ROUND_HALF_EVEN).doubleValue();
	}

	public double getTemperature(String data) {

		String temp = data.substring(2, 4) + data.substring(0, 2);
		int i = Integer.parseInt(temp, 16);
		i = i & (0xDFFF); // 去掉 第13位的正负符号， 只解析0度以上的

		double tempdata = i / 32.0;
		BigDecimal round = new BigDecimal(tempdata);
		return round.setScale(3, BigDecimal.ROUND_HALF_EVEN).doubleValue();

	}

	@Override
	public void setActionId(String actionId) {
		this.actionId = actionId;
	}

	public long getIndicatorDate(String time) {
		return Long.parseLong(time, 16);
	}

	// BCD码时间转换方法
	protected String readTime(String data) {
		String secByte = data.substring(0, 2);
		String sec = secByte.substring(0, 1) + secByte.substring(1, 2);

		String minByte = data.substring(2, 4);
		String min = minByte.substring(0, 1) + minByte.substring(1, 2);

		String hourByte = data.substring(4, 6);
		String hour = hourByte.substring(0, 1) + hourByte.substring(1, 2);

		String dayByte = data.substring(6, 8);
		String day = dayByte.substring(0, 1) + dayByte.substring(1, 2);

		String monthByte = data.substring(8, 10);

		// 不处理week.
		String bi = Integer.toBinaryString(Integer.parseInt(
				monthByte.substring(0, 1), 16));

		String monthBit = "0";
		int len = bi.length();
		if (bi.substring(len - 1, len).equals("1"))
			monthBit = "1";

		String month = monthBit + monthByte.substring(1, 2);

		String yearByte = data.substring(10, 12);
		String year = yearByte.substring(0, 1) + yearByte.substring(1, 2);
		String time = year + "-" + month + "-" + day + " " + hour + ":" + min
				+ ":" + sec;

		return time;

	}

}
